package com.fw.application.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fw.application.controller.base.UserController;
import com.fw.common.Builder;
import com.fw.mes.Result;
import com.fw.system.web.model.entity.TbNotice;
import com.fw.system.web.model.vo.TbNoticeVo;
import com.fw.system.web.service.ITbNoticeService;
import com.fw.system.web.service.ITbUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

import static com.fw.mes.ResultUtils.success;

/**
 * <p>
 * 公告、炸弹护盾使用情况表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2023-03-28
 */
@RestController
@RequestMapping("/web/notice")
@Api(tags = "滚动通知")
@CrossOrigin
public class TbNoticeController implements UserController {


    @Autowired
    private ITbNoticeService noticeService;
    @Autowired
    private ITbUserService userService;

    @ApiOperation("通知列表")
    @GetMapping("/getList")
    public Result<List<TbNoticeVo>> getList() {
        List<TbNoticeVo> collect = noticeService.list(Wrappers.<TbNotice>lambdaQuery().eq(TbNotice::getNoticeType, 1)
                .or(true).eq(TbNotice::getNoticeType, 2).orderByDesc(TbNotice::getCreateTime).last("limit 5")).parallelStream()
                .map(map -> {
                    TbNoticeVo build = Builder.of(TbNoticeVo::new)
                            .build();
                    BeanUtil.copyProperties(map, build);
                    build.setNikeName(userService.getById(map.getUserId()).getNickName());
                    build.setImgUrl(userService.getById(map.getUserId()).getImgUrl());
                    build.setGamePlayerNikeName(userService.getById(map.getGamePlayerUserId()).getNickName());
                    return build;
                }).collect(Collectors.toList());
        return success(collect);
    }

}

